package com.wysql.ascost.controller.info;

import com.wysql.ascost.common.base.BaseController;
import com.wysql.ascost.common.domain.AjaxResult;
import com.wysql.ascost.common.domain.ResultTable;
import com.wysql.ascost.model.custom.Tablepar;
import com.wysql.ascost.model.info.*;
import com.wysql.ascost.service.info.InfoProjectbudgetCsService;
import com.wysql.ascost.service.info.InfoProjectbudgetJsService;
import com.wysql.ascost.service.info.InfoProjectbudgetService;
import com.github.pagehelper.PageInfo;
import com.wysql.ascost.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 工程概算台账Controller
 *
 * @author wysqlauto
 * @ClassName: InfoProjectbudgetController
 * @date 2024-11-22 13:38:55
 */
@Api(value = "工程概算台账")
@Controller
@RequestMapping("/InfoProjectbudgetController")
public class InfoProjectbudgetController extends BaseController {

    private String prefix = "info/infoProjectbudget";
    private String prefixstore = "info/infoProjectStore";
    private String prefixana = "info/infoAnalysis";

    @Autowired
    private InfoProjectbudgetService infoProjectbudgetService;
    @Autowired
    private InfoProjectbudgetCsService infoProjectbudgetCsService;
    @Autowired
    private InfoProjectbudgetJsService infoProjectbudgetJsService;


    /**
     * 工程概算台账页面展示
     *
     * @param model
     * @return String
     * @author wysqlauto
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/view")
    @RequiresPermissions("info:infoProjectbudget:view")
    public String view(ModelMap model) {
        return prefix + "/list";
    }

    /**
     * list集合--未入库
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "工程概算台账", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/list")
    @RequiresPermissions("info:infoProjectbudget:list")
    @ResponseBody
    public ResultTable list(Tablepar tablepar, InfoProjectbudget infoProjectbudget) {
        infoProjectbudget.setIsconfirm(0);
        PageInfo<InfoProjectbudget> page = infoProjectbudgetService.list(tablepar, infoProjectbudget);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 新增跳转
     */
    @ApiOperation(value = "新增跳转", notes = "新增跳转")
    @GetMapping("/add")
    public String add(ModelMap modelMap) {
        return prefix + "/add";
    }

    /**
     * 新增保存
     *
     * @param
     * @return
     */
    //@Log(title = "工程概算台账新增", action = "111")
    @ApiOperation(value = "新增", notes = "新增")
    @PostMapping("/add")
    @RequiresPermissions("info:infoProjectbudget:add")
    @ResponseBody
    public AjaxResult add(@RequestBody InfoProjectbudget infoProjectbudget) {
        infoProjectbudget.setProname(infoProjectbudget.getProname().trim());
        //Todo 这里没做特殊符号处理
        //判别名称是否重复
        Boolean isexist = infoProjectbudgetService.findisexist(infoProjectbudget);
        if (isexist){
            return error("工程名称已存在，无法重复导入");
        } else {
            infoProjectbudget.setIsconfirm(0);
            infoProjectbudget.setFiledate(new Date());
            infoProjectbudget.setCreatetype(2);
            int b = infoProjectbudgetService.insertSelective(infoProjectbudget);
            if (b > 0) {
                return success();
            } else {
                return error();
            }
        }
    }

    /**
     * 工程概算台账删除
     *
     * @param ids
     * @return
     */
    //@Log(title = "工程概算台账删除", action = "111")
    @ApiOperation(value = "删除", notes = "删除")
    @DeleteMapping("/remove")
    @RequiresPermissions("info:infoProjectbudget:remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        int b = infoProjectbudgetService.deleteByPrimaryKey(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }


    /**
     * 修改跳转
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "修改跳转", notes = "修改跳转")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap map) {
        map.put("InfoProjectbudget", infoProjectbudgetService.selectByPrimaryKey(id));
        return prefix + "/edit";
    }

    /**
     * 修改保存
     */
    //@Log(title = "工程概算台账修改", action = "111")
    @ApiOperation(value = "修改保存", notes = "修改保存")
    @RequiresPermissions("info:infoProjectbudget:edit")
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@RequestBody InfoProjectbudget infoProjectbudget) {
        //增加对于编号修改重复性校验
        InfoProjectbudget project = infoProjectbudgetService.selectByPrimaryKey(infoProjectbudget.getId());
        if (StringUtils.isNotBlank(infoProjectbudget.getPronum())&&(!infoProjectbudget.getPronum().equals(project.getPronum()))){
            InfoProjectbudgetExample infoProjectbudgetExample = new InfoProjectbudgetExample();
            infoProjectbudgetExample.createCriteria().andPronumEqualTo(infoProjectbudget.getPronum());
            int size = infoProjectbudgetService.selectByExample(infoProjectbudgetExample).size();
            if (size > 0) {
                return error("项目编码已存在，请重新编辑");
            }
        }
        return toAjax(infoProjectbudgetService.updateByPrimaryKeySelective(infoProjectbudget));
    }

    @ResponseBody
    @RequestMapping(value = {"/uploadBaseSingle"})
    public Map<String, Object> uploadBaseSingle(@RequestParam("file") MultipartFile file,
                                                @RequestParam("type") Integer type) throws Exception {
        System.err.println(type);
        Map<String, Object> map = new HashMap<>();
        //唯一性校验，目前为名字+类型确定唯一不能重复
//        Boolean isexist = infoProjectbudgetService.finddistinct(file, type);
//        if (isexist){
//            map.put("code", 1);
//            map.put("msg", "概算书《"+file.getOriginalFilename()+"》已经导入或存在手动创建数据，无法重复导入");
//            return map;
//        }else {
        return infoProjectbudgetService.uploadBaseSingle(file);
//        }
    }

    /**
     * @Title: uploadBaseBatch
     * @Author: ShengQL
     * @Date: 2025/2/26 9:36
     * @Params: [files, type, batchnum]
     * @Return: Map<String,Object>
     * @Description: 基建类批量导入
     */
    @ResponseBody
    @PostMapping(value = {"/uploadBaseBatch"})
    public Map<String, Object> uploadBaseBatch(@RequestParam("files") MultipartFile[] files,
                                               @RequestParam("type") Integer type,
                                               @RequestParam("batchnum") String batchnum) throws Exception {
        System.err.println("本次导入批次号："+batchnum);
        System.err.println("本次导入概算书类型："+type);
        return infoProjectbudgetService.uploadBatchJj(files,batchnum,type);
    }

    /**
     * @Title: uploadBaseBatch
     * @Author: ShengQL
     * @Date: 2025/2/26 9:36
     * @Params: [files, type, batchnum]
     * @Return: Map<String,Object>
     * @Description: 业扩类批量导入
     */
    @ResponseBody
    @PostMapping(value = {"/uploadYkBatch"})
    public Map<String, Object> uploadYkBatch(@RequestParam("files") MultipartFile[] files,
                                               @RequestParam("type") Integer type,
                                               @RequestParam("batchnum") String batchnum) throws Exception {
        System.err.println("本次导入批次号："+batchnum);
        System.err.println("本次导入概算书类型："+type);
        return infoProjectbudgetService.uploadYkBatch(files,batchnum,type);
    }

    /**
     * @Title: uploadJgBatch
     * @Author: ShengQL
     * @Date: 2025/2/26 9:36
     * @Params: [files, type, batchnum]
     * @Return: Map<String,Object>
     * @Description: 技改类批量导入
     */
    @ResponseBody
    @PostMapping(value = {"/uploadJgBatch"})
    public Map<String, Object> uploadJgBatch(@RequestParam("files") MultipartFile[] files,
                                             @RequestParam("type") Integer type,
                                             @RequestParam("batchnum") String batchnum) throws Exception {
        System.err.println("本次导入批次号："+batchnum);
        System.err.println("本次导入概算书类型："+type);
        return infoProjectbudgetService.uploadJgBatch(files,batchnum,type);
    }
    @ResponseBody
    @RequestMapping(value = {"/uploadYkSingle"})
    public Map<String, Object> uploadYkSingle(@RequestParam("file") MultipartFile file,
                                              @RequestParam("type") Integer type) throws Exception {
        System.err.println(type);
        return infoProjectbudgetService.uploadYkSingle(file);
    }

    @ResponseBody
    @RequestMapping(value = {"/import"})
    public AjaxResult importBatch(String ids) {
        int b = infoProjectbudgetService.importBatch(ids);
        if (b > 0) {
            return success();
        } else {
            return error();
        }
    }

    /**
     * list集合--入库
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "工程概算台账", action = "111")
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/listin")
    @RequiresPermissions("info:infoProjectbudget:list")
    @ResponseBody
    public ResultTable listin(Tablepar tablepar, InfoProjectbudget infoProjectbudget) {
        infoProjectbudget.setIsconfirm(1);
        PageInfo<InfoProjectbudget> page = infoProjectbudgetService.list(tablepar, infoProjectbudget);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 工程概算(已入库)台账页面跳转
     *
     * @param model
     * @return String
     * @author wysqlauto
     */
    @ApiOperation(value = "分页跳转", notes = "分页跳转")
    @GetMapping("/viewstore")
    @RequiresPermissions("info:infoProjectbudget:view")
    public String viewstore(ModelMap model) {
        return prefixstore + "/list";
    }

    /**
     * 基建类导入跳转
     */
    @ApiOperation(value = "基建类导入跳转", notes = "基建类导入跳转")
    @GetMapping("/uploadJj")
    public String uploadJj(ModelMap modelMap) {
        return prefix + "/uploadJj";
    }

    /**
     * 查看导入操作台账跳转
     */
    @ApiOperation(value = "查看导入操作台账跳转", notes = "查看导入操作台账跳转")
    @GetMapping("/findImportDtl")
    public String findImportDtl(ModelMap modelMap) {
        return prefix + "/finddtl";
    }

    /**
     * 业扩类导入跳转
     */
    @ApiOperation(value = "业扩类导入跳转", notes = "业扩类导入跳转")
    @GetMapping("/uploadYk")
    public String uploadYk(ModelMap modelMap) {
        return prefix + "/uploadYk";
    }

    /**
     * 技改类导入跳转
     */
    @ApiOperation(value = "技改类导入跳转", notes = "技改类导入跳转")
    @GetMapping("/uploadJg")
    public String uploadJg(ModelMap modelMap) {
        return prefix + "/uploadJg";
    }

    /**
     * 综合分析分页跳转
     *
     * @param model
     * @return String
     * @author wysqlauto
     */
    @ApiOperation(value = "综合分析分页跳转", notes = "综合分析分页跳转")
    @GetMapping("/viewana")
    @RequiresPermissions("info:infoProjectbudget:viewana")
    public String viewana(ModelMap model) {
        return prefixana + "/list";
    }

    /**
     * 综合分析list
     *
     * @param tablepar
     * @param
     * @return
     */
    //@Log(title = "工程概算台账", action = "111")
    @ApiOperation(value = "综合分析list", notes = "综合分析list")
    @GetMapping("/listana")
    @RequiresPermissions("info:infoProjectbudget:listana")
    @ResponseBody
    public ResultTable listana(Tablepar tablepar, InfoProjectbudget infoProjectbudget) {
        PageInfo<InfoProjectbudget> page = infoProjectbudgetService.listana(tablepar, infoProjectbudget);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * 综合分析详情页跳转（行上按钮）
     *
     * @param id
     * @param
     * @return
     */
    @ApiOperation(value = "综合分析详情页跳转（行上按钮）", notes = "综合分析详情页跳转（行上按钮）")
    @GetMapping("/viewdtl/{id}")
    public String viewdtl(@PathVariable("id") String id, ModelMap map) {
        map.put("id", id);
        return prefixana + "/viewdtl";
    }

    /**
     * 综合分析list
     *
     * @param
     * @param
     * @return
     */
    //@Log(title = "工程概算台账", action = "111")
    @ApiOperation(value = "综合分析list", notes = "综合分析list")
    @GetMapping("/viewdtlList/{id}")
    @ResponseBody
    public ResultTable viewdtlList(@PathVariable("id") String id) {

        InfoProjectbudget infoProjectbudget = infoProjectbudgetService.selectByPrimaryKey(id);
        InfoProjectbudgetCs infoProjectbudgetCs = null;
        InfoProjectbudgetJs infoProjectbudgetJs = null;

//        String proname = infoProjectbudget.getProname();
        String pronum = infoProjectbudget.getPronum();
        InfoProjectbudgetCsExample infoProjectbudgetExample = new InfoProjectbudgetCsExample();
        infoProjectbudgetExample.createCriteria().andPronumEqualTo(pronum);
        List<InfoProjectbudgetCs> infoProjectbudgetCsList = infoProjectbudgetCsService.selectByExample(infoProjectbudgetExample);
        if (infoProjectbudgetCsList != null && infoProjectbudgetCsList.size() > 0) {
            infoProjectbudgetCs = infoProjectbudgetCsList.get(0);
        }


        InfoProjectbudgetJsExample infoProjectbudgetJsExample = new InfoProjectbudgetJsExample();
        infoProjectbudgetJsExample.createCriteria().andPronumEqualTo(pronum);
        List<InfoProjectbudgetJs> infoProjectbudgetJsList = infoProjectbudgetJsService.selectByExample(infoProjectbudgetJsExample);
        if (infoProjectbudgetJsList != null && infoProjectbudgetJsList.size() > 0) {
            infoProjectbudgetJs = infoProjectbudgetJsList.get(0);
        }


        PageInfo<InfoProjectbudget> page = infoProjectbudgetService.viewdtlList(infoProjectbudget,infoProjectbudgetCs,infoProjectbudgetJs);
        return pageTable(page.getList(), page.getTotal());
    }

    /**
     * @Title: importbatch
     * @Author: ShengQL
     * @Date: 2025/3/17 11:09
     * @Params: [file]
     * @Return: Map<String, Object>
     * @Description: 导入单个文件多条数据
     */
    @ResponseBody
    @RequestMapping(value = {"/uploadBindNum"})
    public Map<String, Object> uploadBindNum(@RequestParam("file") MultipartFile file) throws Exception {
        return infoProjectbudgetService.uploadBindNum(file);
    }

    /**
     * 新增时检查项目编码唯一
     *
     * @return
     */
    @ApiOperation(value = "新增时检查项目编码唯一", notes = "新增时检查项目编码唯一")
    @PostMapping("/checkOrderUniqueAdd")
    @ResponseBody
    public int checkOrderUniqueAdd(InfoProjectbudget infoProjectbudget) {
        int b = 1;
        if (StringUtils.isNotBlank(infoProjectbudget.getPronum())){
            b = infoProjectbudgetService.checkOrderUniqueAdd(infoProjectbudget);
        }
        if (b > 0) {
            return 1;
        } else {
            return 0;
        }
    }

//    /**
//     * 新增时检查项目编码唯一
//     *
//     * @return
//     */
//    @ApiOperation(value = "新增时检查项目编码唯一", notes = "新增时检查项目编码唯一")
//    @PostMapping("/checkOrderUniqueUpdate")
//    @ResponseBody
//    public int checkOrderUniqueUpdate(InfoProjectbudget infoProjectbudget) {
//        int b = 1;
//        if (StringUtils.isNotBlank(infoProjectbudget.getPronum())){
//            b = infoProjectbudgetService.checkOrderUniqueUpdate(infoProjectbudget);
//        }
//        if (b > 0) {
//            return 1;
//        } else {
//            return 0;
//        }
//    }

}
